﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/6/29 21:51:22
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LEARNREN.COM.TOOL
{
    public static class UtilityTool
    {
        private static string UsersFolder
        {
            get
            {
                return GetConfiguration("usersFolder");
            }
        }
        private static string PhysicalUsersFolder
        {
            get
            {
                return System.Web.Hosting.HostingEnvironment.MapPath(UsersFolder);
            }
        }

        /// <summary>
        /// 获取相对于根目录的新的图片地址，第一个地址是虚拟路径，第二个地址是物理路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string[] GetNewPicPath(string filePath)
        {
            if (Directory.Exists(PhysicalUsersFolder) == false)
            {
                Directory.CreateDirectory(PhysicalUsersFolder);
            }

            string ext = Path.GetExtension(filePath);
            string timeStr = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            string path1 = string.Format("{0}{1}{2}", UsersFolder, timeStr, ext);
            string path2 = string.Format("{0}{1}{2}", PhysicalUsersFolder, timeStr, ext);

            return new string[] { path1, path2 };
        }

        /// <summary>
        /// 获取与指定的图片地址对应的缩略图地址，第一个地址是虚拟路径，第二个地址是物理路径
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static string[] GetIcoPicPath(string imagePath)
        {
            if (Directory.Exists(PhysicalUsersFolder) == false)
            {
                Directory.CreateDirectory(PhysicalUsersFolder);
            }
            string fileName = Path.GetFileName(imagePath);
            string path1 = string.Format("{0}{1}{2}", UsersFolder, "ico_", fileName);
            string path2 = string.Format("{0}{1}{2}", PhysicalUsersFolder, "ico_", fileName);
            return new string[] { path1, path2 };
        }

        /// <summary>
        /// 生成缩略图，如果指定的比例不对则会变形。发生异常直接抛出。
        /// </summary>
        /// <param name="sourceImagePath">源图像物理路径</param>
        /// <param name="targetImagePath">要保存的图像物理路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static void GetThumbnailImage(string sourceImagePath, string targetImagePath, int width, int height)
        {
            System.Drawing.Image ResourceImage = System.Drawing.Image.FromFile(sourceImagePath);
            System.Drawing.Image ReducedImage;
            System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(() => false);

            ReducedImage = ResourceImage.GetThumbnailImage(width, height, callb, IntPtr.Zero);
            ReducedImage.Save(targetImagePath);
            ReducedImage.Dispose();
            ResourceImage.Dispose();
        }

        /// <summary>
        /// 从配置文件获取配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfiguration(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }
    }
}
